#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <unistd.h>
#include "server.h"
#include "Parser.h"
#include "Consumer.h"

int main(int argc, char** argv) {
	if (argc != 4) {
		fprintf(stderr, "Usage: ./%s <query number> <path to query ascii protocol buffers file>\n", argv[0]);
		return 1;
	}

	const int queryNumber = atoi(argv[1]);
  const int data_size = atoi(argv[3]);

	Parser parser;
	int fd = open(argv[2], O_RDONLY);
	if (fd == -1) {
		fprintf(stderr, "Error occured while opening query file.\n");
		return 1;
	}

	Server* server = CreateServer(queryNumber, data_size);
	Consumer* consumer = parser.parseOperationsTree(fd, server);

	if (consumer == 0) {
		fprintf(stderr, "Error occured while parsing query file.\n");
		delete server;
		return 1;
	}
	close(fd);

	consumer->consume();

	delete consumer;
	delete server;

	return 0;
}
